var layer;
var $;
var element;
var form;
var laydate;
var table;
layui.use(['element','layer','laydate','form','table'], function(){
    layer = layui.layer;
    $ = layui.jquery;
    element = layui.element;
    form = layui.form;
    laydate = layui.laydate;
    table = layui.table;
    load();
    selIndexUserTab();
})
/**
 *  点击事件  刷新  返回
 *  高级检索
 * */
function load(){
    $(".tit_btn").bind('click',function(){
        location.reload();
    })
    $(".tit_btn2").bind('click',function(){
        window.history.go(-1);  //返回上一页
    })
    $("#index_btn1").bind('click',function(){
        layer.open({
            type:2,//弹出层的类型
            title:"高级检索",
            area:['550px','350px'],//弹出层大小
            shade:0,//设置遮罩
            offset:'auto',//设置遮罩的位置
            maxmin:true,
            //弹出层内容
            content :"/personnel/layer/PerUserList_Think_layer_index.html",
            btn:['确定','取消'],
            yes:function(index,layero){
                var body = layero.find('iframe').contents();
                var form = $(body).find("#selForm").serialize();
                var studyDate = body.find("input[name=studyDate]").val();
                var integration = body.find("input[name=integration]").val();
                var type = body.find("select[name=type]").val();
                var status = body.find("select[name=status]").val();
                //判断字段是否为空
                if(!studyDate){
                    form+="&studyDate="+0
                }else{
                    form+="&studyDate="+studyDate
                }
                if(!integration){
                    form+="&integration="+0
                }else{
                    form+="&integration="+integration
                }
                if(!type){
                    form+="&type="+0
                }else{
                    form+="&type="+type
                }
                if(!status){
                    form+="&status="+0
                }else{
                    form+="&status="+status
                }
                selIndexUserTab(form);
                layer.close(layer.index);
            },btn2:function(index,layero){

            }
        })
    })
    //点击搜索
    $("#indexBtn").bind('click',function(){
        selIndexUserTab();
        layer.msg("搜索成功", {icon: 1});
    })
    //点击赠送优惠券
    $("#index_btn2").bind('click',function(){
        giveAwayCoupons();

    })
}
/**
 *  赠送优惠券
 */
function giveAwayCoupons(){
    var checkStatus = table.checkStatus('userListTab');
    var ids = [];
    $(checkStatus.data).each(function (i, o) {//o即为表格中一行的数据
        ids.push(o.userId);
    });
    ids = ids.join(",");
    if (ids.length < 1) {
        layer.msg('请选择所要赠送的学生', {icon: 7});
        return false;
    }else{
        layer.open({
            type:2,//弹出层的类型
            title:"赠送优惠券",
            area:['980px','540px'],//弹出层大小
            shade:0,//设置遮罩
            offset:'auto',//设置遮罩的位置
            maxmin:true,
            //弹出层内容
            content :"/personnel/layer/PerUserList_Think_layer_GiveAwayCoupons.html",
            success:function(layero,index){
                var body = layero.find('iframe').contents();
                body.find("input[name=userId]").val(ids);
            }
        })
    }
}


/**
 *  检索用户列表
 */
function selIndexUserTab(vals){
    var studentNumber = $("input[name=studentNumber]").val();
    var account = $("input[name=account]").val();
    var profession = $("input[name=profession]").val();
    var realName = $("input[name=realName]").val();
    if(!studentNumber){
        vals+="&studentNumber="+0
    }else{
        vals+="&studentNumber="+studentNumber
    }
    if(!account){
        vals+="&account="+''
    }else{
        vals+="&account="+account
    }
    if(!profession){
        vals+="&profession="+''
    }else{
        vals+="&profession="+profession
    }
    if(!realName){
        vals+="&realName="+''
    }else{
        vals+="&realName="+realName
    }
    $.ajax({
        url:"/personnel/selThinkUserTable",
        type:"post",
        dataType:"json",
        data:vals,
        success:function(item) {
            var data = item.obj;
            userTab(data);
        }
    })
}
function userTab(item){
    table.render({
        elem:"#userListTab",//容器
        title:"用户数据列表",
        limit:10,//分页显示条数
        width:1225,
        data:item,
        toolbar:true,
        page:true,
        cols:[[//定义列
            {type:'checkbox',align:'center',width:40},//复选框靠左
            {title:'序号',align:'center',width:60,templet:function(data){
                    return data.LAY_INDEX;
                }},
            {field:"studentNumber",title:"用户学号",align:'center',width:110},
            {field:"",title:"用户照片",align:'center',width:97,templet:function(data){
                    return "<img src=\""+data.avatarUrl+data.photoImg+"\" style='width:60px;height:60px;'/>";
                }},
            {field:"account",title:"用户账号",align:'center',width:125},
            {field:"realName",title:"真实姓名",align:'center',width:110},
            {field:"",title:"学习时长",align:'center',width:110,templet:function(data){
                if(data.studyDate){
                    return data.studyDate+" 分钟";
                }else{
                    return "0 分钟";
                }
            }},
            {field:"profession",title:"所属专业",align:'center',width:120},
            {field:"",title:"状态",align:'center',width:105,templet:function(data){
                    if(data.type==2){
                        return"<a style='display:inline-block;'><input type=\"checkbox\" disabled  checked lay-skin=\"switch\"  lay-filter='filter' lay-text='在读|已毕业'></a>"
                    }else if(data.type==3){
                        return"<a style='display:inline-block;'><input type=\"checkbox\" disabled  lay-skin=\"switch\" lay-filter='filter' lay-text='在读|已毕业'></a>"
                    }
                }},
            {field:"",title:"账号状态",align:'center',width:100,templet:function(data){
                    if(data.status==2){
                        return"<a style='display:inline-block;'><input type=\"checkbox\" disabled  checked lay-skin=\"switch\"  lay-filter='filter' lay-text='正常|停用'></a>"
                    }else if(data.status==3){
                        return"<a style='display:inline-block;'><input type=\"checkbox\" disabled  lay-skin=\"switch\" lay-filter='filter' lay-text='正常|停用'></a>"
                    }
                }},
            {field:"",title:"量化积分",align:'center',width:90,templet:function(data){
                    return data.integration+"分";
                }},
            {field:"",title:"操作",align:'center',width:145,templet:function(data){
                    return "<a style='color:#009688;cursor:pointer;' onclick='selUserThink(\""+data.developmentId+"\");'>查看</a>   " +
                        "<a style='color:#009688;cursor:pointer;' onclick='updUserThink(\""+data.developmentId+"\");'>编译</a>   " ;
                }}
        ]]
    });
}
/**
 *  点击查看
 */
function selUserThink(developmentId){
    $.ajax({
        url:"/personnel/selUserThinkIndex",
        type:"post",
        dataType:"json",
        data:{"developmentId":developmentId},
        success:function(item) {
            var data = item.obj;
            selUserThinkLayer(data);
        }
    })
}
function selUserThinkLayer(data){
    var tablename = "进思用户数据列表";
    var type=5;
    var remark = data.developmentId+"";
    addOperLog(tablename,type,remark);
    layer.open({
        type:2,//弹出层的类型
        title:"高级检索",
        area:['1100px','650px'],//弹出层大小
        shade:0,//设置遮罩
        offset:'auto',//设置遮罩的位置
        maxmin:true,
        //弹出层内容
        content :"/personnel/layer/PerUserList_Think_layer_info.html",
        btn:['关闭','取消'],
        yes:function(index,layero){
            layer.close(layer.index);
        },btn2:function(index,layero){

        },success:function(layero,index){
            var obj = layero.find('iframe').contents();
            obj.find(".account").html(data.account);
            obj.find(".nickname").html(data.nickname);
            obj.find("input[name=nickname]").val(data.nickname);
            obj.find("input[name=studyDate]").val(data.studyDate+"分钟");
            obj.find("input[name=integration]").val(data.integration+"分");
            obj.find("input[name=avatarUrl]").val(data.avatarUrl);
            obj.find("input[name=photoImg]").val(data.photoImg);
            obj.find("input[name=studentNumber]").val(data.studentNumber);
            obj.find("input[name=profession]").val(data.profession+"学院");
            if(data.type==2){
                obj.find("input[name=type]").val("在读");
            }else if(data.type==3){
                obj.find("input[name=type]").val("已毕业");
            }
            obj.find("textarea[name=studyContent]").val(data.studyContent);
            obj.find("input[name=entranceDate]").val(data.entranceDate);
            obj.find("input[name=graduationDate]").val(data.graduationDate);
            if(data.status==2){
                obj.find("input[name=status]").val("账号正常");
            }else if(data.status==3){
                obj.find("input[name=status]").val("账号处于封禁状态");
            }
            obj.find("input[name=realName]").val(data.realName);
            obj.find("input[name=idCard]").val(data.idCard);
            obj.find("input[name=phone]").val(data.phone);
            if(data.sex==2){
                obj.find("input[name=sex]").val("男");
            }else if(data.sex==3){
                obj.find("input[name=sex]").val("女");
            }
            obj.find("input[name=email]").val(data.email);
            obj.find("input[name=createTime]").val(data.createTime);
            obj.find("input[name=consumption]").val(data.consumption+"元");
        }
    })
}
/**
 *  点击展示所有的信息  点击编辑开始编辑信息
 */
function updUserThink(developmentId){
    $.ajax({
        url:"/personnel/selUserThinkIndex",
        type:"post",
        dataType:"json",
        data:{"developmentId":developmentId},
        success:function(item) {
            var data = item.obj;
            updUserThinkLayer(data);
        }
    })
}
function updUserThinkLayer(data){
    layer.open({
        type:2,//弹出层的类型
        title:"高级检索",
        area:['1100px','650px'],//弹出层大小
        shade:0,//设置遮罩
        offset:'auto',//设置遮罩的位置
        maxmin:true,
        //弹出层内容
        content :"/personnel/layer/PerUserList_Think_layer_info_upd.html",
        success:function(layero,index){
            var obj = layero.find('iframe').contents();
            obj.find(".account").html(data.account);
            obj.find(".nickname").html(data.nickname);
            obj.find("input[name=nickname]").val(data.nickname);
            obj.find("input[name=studyDate]").val(data.studyDate+"分钟");
            obj.find("input[name=integration]").val(data.integration+"分");
            obj.find("input[name=avatarUrl]").val(data.avatarUrl);
            obj.find("input[name=photoImg]").val(data.photoImg);
            obj.find("input[name=studentNumber]").val(data.studentNumber);
            obj.find("input[name=profession]").val(data.profession);
            obj.find("select[name=type]").val(data.type);
            obj.find("textarea[name=studyContent]").val(data.studyContent);
            obj.find("input[name=entranceDate]").val(data.entranceDate);
            obj.find("input[name=graduationDate]").val(data.graduationDate);
            if(data.status==2){
                obj.find("input[name=status]").val("账号正常");
            }else if(data.status==3){
                obj.find("input[name=status]").val("账号处于封禁状态");
            }
            obj.find("input[name=realName]").val(data.realName);
            obj.find("input[name=idCard]").val(data.idCard);
            obj.find("input[name=phone]").val(data.phone);
            obj.find("select[name=sex]").val(data.sex);
            obj.find("input[name=email]").val(data.email);
            obj.find("input[name=createTime]").val(data.createTime);
            obj.find("input[name=consumption]").val(data.consumption+"元");
            obj.find("input[name=developmentId]").val(data.developmentId);
            obj.find("input[name=userId]").val(data.userId);
        }
    })
}
/**
 *  添加后台操作日志
 * */
function addOperLog(tablename,type,remark){
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    var mid = create.userId;
    form+="&mid="+mid;
    form+="&tablename="+tablename;
    form+="&type="+type;
    form+="&remark="+remark;
    $.post("/personnel/addOperationLog", form, function (res) {
        form="";
    });
}